package org.database;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;


import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import javax.swing.border.EmptyBorder;

import org.omg.CORBA.PUBLIC_MEMBER;





public class DatabaseTool {	
	 private String username;
	  private String password;
	  private String recordinfo;
	  private String studentname;
	  private String dormname;
	  private String department;
	  private String profession;
	  private String sex;
	//登录验证
   public  boolean Vertifylogin(String username1,String password1){
	     int id=0;
	    username=username1;
	    password=password1;
		try{
			Driver driver=new com.mysql.jdbc.Driver();
			DriverManager.registerDriver(driver);
			Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8","root","123456");
			String sql="select * from user where username=? and password=?";
			PreparedStatement stmt=connection.prepareStatement(sql);
			stmt.setString(1,username);
			stmt.setString(2, password);
		   ResultSet  rs=stmt.executeQuery();
		    while(rs.next()){
		 	id=rs.getInt("id");	 
		    }  
		    rs.close();
			stmt.close();
			connection.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println(id);
		if(id>0){
			return true;
		}else{
			return false;
		}
   
   }
   //插入注册信息
  public boolean insertStudentInfo(String username1,String password1,String recordinfo1,String studentname1,String dormname1,String department1,String profession1,String sex1){
	   int  rs = 0;
	   System.out.println(recordinfo1);
	   try{
			Driver driver=new com.mysql.jdbc.Driver();
			DriverManager.registerDriver(driver);
			Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8","root","123456");
			String sql="insert into user(username, password, recordinfo, studentname, dormname, department, profession, sex) values(?,?,?,?,?,?,?,?)";
		   PreparedStatement preparedStatement=connection.prepareStatement(sql);
		   preparedStatement.setString(1,username1);
		   preparedStatement.setString(2,password1);
		   preparedStatement.setString(3,recordinfo1);
		   preparedStatement.setString(4,studentname1);
		   preparedStatement.setString(5,dormname1);
		   preparedStatement.setString(6,department1);
		   preparedStatement.setString(7,profession1);
		   preparedStatement.setString(8,sex1);
		   rs=preparedStatement.executeUpdate();
		   preparedStatement.close();
			connection.close();
		}catch(Exception e){
			e.printStackTrace();
		}
       if(rs==1){
    	   return true;
       }else{
    	   return false;
       }
   }
   //找回密码
  public String retrievePassword(String recordinfo,String username1){
	  String password = null;
	  try{
			Driver driver=new com.mysql.jdbc.Driver();
			DriverManager.registerDriver(driver);
			Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8","root","123456");
			String sql="select password from user where recordinfo=? and username=? ";
			PreparedStatement stmt=connection.prepareStatement(sql);
			stmt.setString(1,recordinfo);
			stmt.setString(2,username1);
		   ResultSet  rs=stmt.executeQuery();
		    while(rs.next()){
	        password=rs.getString(1);
		    }  
		    rs.close();
			stmt.close();
			connection.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	  return  password;
  }
  //修改密码
  public boolean updatePassword(String password1,String recordinfo,String username){
	   int  rs1= 0;
	  try{
			Driver driver=new com.mysql.jdbc.Driver();
			DriverManager.registerDriver(driver);
			Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8","root","123456");
			String sql="update  user set password=? where recordinfo=? and username=?";
		 PreparedStatement stmt=connection.prepareStatement(sql);
			stmt.setString(1,password1);
			stmt.setString(2,recordinfo);
			stmt.setString(3,username);
		   rs1=stmt.executeUpdate();
			stmt.close();
			connection.close();
		}catch(Exception e){
			e.printStackTrace();
		} 
	   if(rs1==1){
    	   return true;
       }else{
    	   return false;
       }
  }
  
  public static void main(String[] args){
	  DatabaseTool databaseTool=new DatabaseTool();
	  System.out.println(databaseTool.updatePassword("1234","123","1440112131"));
  }
}
